package com.abe.signature;


import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class WebServiceActivity extends Activity {

    private TextView textView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_service);
        textView = findViewById(R.id.item_text);
        findViewById(R.id.item_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new main()).start();
            }
        });
    }

    class main implements Runnable {

        @Override
        public void run() {
            getRemoteInfo();
        }
    }

    /**
     * 得到远程数据
     */
    private void getRemoteInfo() {
        //String nameSpace = "http://WebXml.com.cn/";//命名空间
        String nameSpace = "http://WebXml.com.cn/";//命名空间
        //String methodName = "getMobileCodeInfo";//调用的方法名
        String methodName = "getOpenFundString";//调用的方法名
        //String endPoint = "http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx";
        String endPoint = "http://ws.webxml.com.cn/WebServices/ChinaOpenFundWS.asmx";
        //String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";
        String soapAction = "http://WebXml.com.cn/getOpenFundString";
        //指定webservice的命名空间和调用方法
        SoapObject rpc = new SoapObject(nameSpace,methodName);
        //需要传入的参数
        //rpc.addProperty("theCityName","郑州");
        //rpc.addProperty("",null);
        //生产调用webService方法的SOAP请求信息,并指定SOAP的版本 ,这个版本 根据 asdl文件中是1.1还是1.2协议来指定的. 一般VER11或者VER12都可以,以前还有VER10
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
        envelope.bodyOut = rpc;
        envelope.dotNet = true;//设置是否调用的是dotNet开发的webService
        //(new MarshalBase64()).register(envelope);
        // 等价于envelope.bodyOut = rpc;
        envelope.setOutputSoapObject(rpc);
        HttpTransportSE transportSE = new HttpTransportSE(endPoint);
        transportSE.debug = true;
        try {
            //调用webService
            transportSE.call(soapAction,envelope);
            transportSE.call(null,envelope);
            if(envelope.getResponse()!=null){
                //获取返回的结果
//                SoapObject object = (SoapObject) envelope.bodyIn;
//                List<String> list= new ArrayList<>();
//                int count = object.getPropertyCount();
//                for (int i=0;i<count;i++){
//                    SoapObject subObject = (SoapObject) object.getProperty(i);
//                    String name = subObject.getPropertyAsString(i);
//
//                    list.add(name);
//                }
//                Log.e("集合长度",list.size()+"");
                String result = String.valueOf(envelope.getResponse());
                System.out.println("返回结果--------->"+result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
